package com.wyg.epdata.controller;

import com.alibaba.fastjson.JSONObject;
import com.wyg.epdata.common.constant.BaseConstant;
import com.wyg.epdata.entity.reqDto.EntityReq;
import com.wyg.epdata.entity.reqDto.ExcelParamDTO;
import com.wyg.epdata.service.CompanyNumService;
import com.wyg.epdata.service.FileService;
import com.wyg.epdata.util.DateUtil;
import com.wyg.epdata.util.file.FileUtil;
import com.wyg.epdata.util.file.HtmlTemplate;
import com.wyg.epcommonbase.common.ApiResult;
import com.wyg.epcommonbase.common.annotation.AuthRoles;
import com.wyg.epcommonbase.common.cache.Cache;
import com.wyg.epcommonbase.common.cache.impl.CurrentCache;
import com.wyg.epcommonbase.common.enums.SdfEnum;
import com.wyg.epcommonbase.common.util.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: WangYongGan
 * @Date: 2021/11/9 1:33
 * @Description:
 */
@Api(tags = "文件操作")
@Slf4j
@RestController
@RequestMapping("file")
public class FileController {
    Cache cache = new CurrentCache();
    @Autowired
    FileService fileService;
    @Autowired
    CompanyNumService companyNumService;

    @SneakyThrows
//    @AuthRoles("用户&会员")
    @ApiOperation("保存pdf")
    @PostMapping("downloadPdf")
    public void downloadPdf(@RequestBody EntityReq req, HttpServletResponse response) {
        String entity = req.getEntity();
        AssertUtil.isTrue(!companyNumService.exists(entity), 10017, "企业不存在");
        final SimpleDateFormat sdf = DateUtil.getSDF(SdfEnum.YMD);
        String fileNamePrefix = entity + "-" + sdf.format(new Date());
        String pdfPath;
        final Object o = cache.get(fileNamePrefix);
        if (o != null) {
            pdfPath = (String) o;
        } else {
            pdfPath = BaseConstant.fileLoadPath + File.separator + "pdf" + File.separator + fileNamePrefix + ".pdf";
            File file = new File(pdfPath);
            if (!file.exists()) {
                final JSONObject[] jsons = fileService.downPdf(req, response);
                HtmlTemplate.begin(jsons[0].toJSONString(), jsons[1].toJSONString(), pdfPath);
            }
            cache.set(fileNamePrefix, pdfPath);
        }

        log.info("下载: " + pdfPath);
        FileUtil.downloadFile(pdfPath, "analysis.pdf", "utf-8", response);
    }

    @SneakyThrows
    @ApiOperation("下载excel")
    @PostMapping("downloadExcel")
    public ApiResult downloadExcel(@RequestBody ExcelParamDTO excelParam, HttpServletResponse response) {
        fileService.downExcel(excelParam, response);
        return null;
    }


}

